package chapter04

import scala.util.control.Breaks

/**
  * create by unseRdoG on  2021-05-28  18:25
  */
object Test04_Break {

  def main(args: Array[String]): Unit = {

    /**
      * 使用异常来终止循环
      **/
    try {
      for (i <- 1 until 5) {
        if (i == 3) throw new RuntimeException
        println(i)
      }
    } catch {
      case e: Exception => //  什么都不做只是退出循环
    }



    System.out.println("=====================================================================================================")

    /**
    *  使用 Break 来终止循环
      *
      *  使用 breakable 表明当前循环可中断
      *
    * */
    Breaks.breakable(
      for (i <- 1 to 5){
        if (i == 3)
          Breaks.break()
        println(i)
      }
    )
  }
}
